﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using TCPIPControl.StructHelper;

namespace Common.GeneralClass
{
    public class DisplacementDataInfo
    {
        public long Id;
        public short ChannelId;
        public float PK2PK;
        public float[] Amps=new float[512];
        public float[] Phs = new float[512];
        public float[] ZDs = new float[1024];
        public float RoomMeanSquareValue;
        public float OneMultiplierAmp;
        public float OneMultiplierPhase;
        public float TWOMultiplierAmp;
        public float TWOMultiplierPhase;
        public float HalfMultiplierAmp;
        public float HalfMultiplierPhase;
        public float FreOption1;
        public float FreOption2;
        public float FreOption3;
        public byte AlarmFlag;
        public DateTime InsertTime;
        public short ProjectId;
        public float GapVoltage;
        public float Rpm;
    }
    public class OperateDisplacementDataInfo 
    {
    
            MyDb mydb = new MyDb(Mdl_Class.Systemset.DBConnectionString());
            MyDb mydb_insert = new MyDb(Mdl_Class.Systemset.DBConnectionString());
            DateTime dateBegin = DateTime.Now;
            int insertcount = 0;
            public int AddDisplacementDataInfo(DisplacementData displacementData)
            {
                int i = 0;
                using (mydb_insert)
                {
                    try
                    {
                        mydb_insert.Open();
                        Stopwatch sp = new Stopwatch();
                        sp.Start();
                        DateTime nowtime = Convert.ToDateTime(displacementData.Year.ToString() + "-" + displacementData.Month.ToString() + "-" + displacementData.Day.ToString() + " " + displacementData.Hour.ToString() + ":" + displacementData.Minute.ToString() + ":" + displacementData.Second.ToString());
                        long rpmid = Mdl_Class.RpmId;
                        if (rpmid > 0)
                        {
                            i = mydb_insert.ExecuteNonQuery_DisplacementData(rpmid, "T_DisplacementData", nowtime, displacementData);

                            TimeSpan ts1 = new TimeSpan(dateBegin.Ticks);
                            TimeSpan ts2 = new TimeSpan(nowtime.Ticks);
                            TimeSpan ts3 = ts1.Subtract(ts2).Duration();
                            if (ts3.TotalSeconds > 60)
                            {
                                i = mydb_insert.ExecuteNonQuery_DisplacementData(rpmid, "T_DisplacementData_Day", nowtime, displacementData);
                                dateBegin = nowtime;
                            }
                            insertcount++;
                        }
                        sp.Stop();
                        Console.WriteLine("位移 总计耗时:" + sp.ElapsedMilliseconds + " 插入条数：" + insertcount);
                        mydb_insert.Close();
                    }
                    catch (Exception ex)
                    {
                        Mdl_Class.WriteLog.dbErrLog("AddDisplacementDataInfo()", ex.ToString());
                    }
                }
                return i;
            }

            /// <summary>
            /// 通过字段名查询
            /// </summary>
            public List<float> GetDataByFieldName(string FieldName, int ProjectId, int channelId, string dateTime_Start, string dateTime_End, long RpmId, int alarmFlag)
            {
                List<float> dataslist = new List<float>();
                using (mydb)
                {
                    try
                    {

                        mydb.Open();
                        string sql = string.Format("select {0} from {1} where 1=1 ", FieldName, "T_DisplacementData");



                        if (ProjectId > 0)
                        {
                            sql = sql + string.Format(" and projectId={0}", ProjectId);
                        }
                        if (RpmId > 0)
                        {
                            sql = sql + string.Format(" and RpmId={0}", RpmId);
                        }
                        if (alarmFlag >= 0)
                        {
                            sql = sql + string.Format(" and AlarmFlag={0}", alarmFlag);
                        }
                        if (channelId >= 0)
                        {
                            sql = sql + string.Format(" and ChannelId={0}", channelId);
                        }

                        if (dateTime_Start != "")
                        {
                            sql = sql + string.Format(" and InsertTime between '{0}' and '{1}'", dateTime_Start, dateTime_End);
                        }

                        sql = sql + " order by Id";
                        mydb.ExecuteReader(sql);
                        while (mydb.DataReader.Read())
                        {
                            float testvalue = 0.0f;
                            if (mydb.DataReader[FieldName] != null)
                            {
                                testvalue = Convert.ToSingle(mydb.DataReader[FieldName].ToString());
                            }
                            dataslist.Add(testvalue);
                        }
                        mydb.Close();
                    }
                    catch (Exception ex)
                    {
                        dataslist.Clear();
                        Mdl_Class.WriteLog.dbErrLog("OperateDisplacementDataInfo.GetDataByFieldName(string FieldName, int ProjectId, int channelId,string dateTime_Start, string dateTime_End, long RpmId,int alarmFlag) ", ex.ToString());
                    }
                }

                return dataslist;
            }



    }
}
